﻿using FieldTools.Client.Command;
using FieldTools.Client.Model.Window;
using FieldTools.Client.Tools;
using FieldTools.Util;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;

namespace FieldTools.Client.ViewModel.Window
{
    /// <summary>
    /// 正则视图模型
    /// </summary>
    public class RegexViewModel : NotificationObject, ISingletonDependency
    {
        private readonly ILogger _logger;
        /// <summary>
        /// 
        /// </summary>
        public RegexViewModel(ILogger<RegexViewModel> logger)
        {
            _logger = logger;
        }

        #region 实体
        RegexModel obj = new();
        /// <summary>
        /// 正则字符串
        /// </summary>
        public string? RegexStr
        {
            get { return obj.RegexStr; }
            set { obj.RegexStr = value; this.RaisePropertyChanged(nameof(RegexStr)); }
        }

        /// <summary>
        /// 原始字符串
        /// </summary>
        public string? RawStr
        {
            get { return obj.RawStr; }
            set { obj.RawStr = value; this.RaisePropertyChanged(nameof(RawStr)); }
        }

        private string? outStr;
        /// <summary>
        /// 输出字符串
        /// </summary>
        public string? OutStr
        {
            get { return outStr; }
            set { outStr = value; this.RaisePropertyChanged(nameof(OutStr)); }
        }


        #endregion

        #region 事件
        private BaseCommand? execute;
        /// <summary>
        /// 执行解析
        /// </summary>
        public BaseCommand Execute
        {
            get
            {
                if (execute == null)
                {
                    execute = new BaseCommand(new Action<object>(o =>
                    {
                        ExecuteRegex();
                    }));
                }
                return execute;
            }
        }
        #endregion

        #region 方法
        private void ExecuteRegex()
        {
            OutStr = string.Empty;
            if (!string.IsNullOrEmpty(RegexStr) && !string.IsNullOrEmpty(RawStr))
            {
                Regex regex = new(RegexStr);
                if (regex.IsMatch(RawStr))
                {
                    var test = regex.Matches(RawStr);
                    foreach (Match match in test)
                    {
                        OutStr += match.Value + "\r\n";
                    }
                }
                else
                {
                    MessageBox.Show("正则未匹配到数据");
                }
            }
            else
            {
                MessageBox.Show("请输入要解析的文本数据");
            }
        }
        #endregion
    }
}
